package com.blue.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blue.ssm.bean.UserModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class TestController {
    @RequestMapping("/toTest.do")
    public String toTest(HttpServletRequest request) {
        System.out.println("进入toTest方法");
        request.setAttribute("name", "小明");
        return "test";
    }

    @ResponseBody
    @RequestMapping("/userInfo.do")
    public String userInfo() {
        Map<String, String> student = new HashMap<String, String>();
        student.put("id", "1001");
        student.put("name", "zhangsan");
        String jsonStr = JSON.toJSONString(student);
        return jsonStr;
    }

    @ResponseBody
    @RequestMapping("/mapTest.do")
    public String mapTest() {
        Map<String, String> m = new HashMap<String, String>();
        m.put("1", "张三");
        m.put("2", "王五");
        m.put("3", "张三");
        String str = new String();
        for (Map.Entry<String, String> entry : m.entrySet()) {
            if (entry.getValue().equalsIgnoreCase("张三")) {
//                m.remove(entry.getKey());
                continue;
            }
            str = str + "; key=" + entry.getKey() + "and value=" + entry.getValue();
        }
        String mapStr = JSON.toJSONString(m);
        return mapStr;
    }

    @ResponseBody
    @RequestMapping(value = "/listTest.do", method = RequestMethod.GET)
    public String listTest(Integer id) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        arrayList.add(60);
        arrayList.add(70);
        arrayList.add(50);
        arrayList.add(80);
        if (id != null && id.intValue() != 0) {
            arrayList.add(id);
        }
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("arrayList", arrayList);
        jsonObj.put("linkedList", linkedList);
        String jsonStr = JSON.toJSONString(jsonObj);
        return jsonStr;
    }

    @ResponseBody
    @RequestMapping(value = "/addUser.do", method = RequestMethod.GET)
    public String addUser(UserModel user) {

        Map<String, String> newUser = new HashMap<String, String>();
        newUser.put("name", user.getUsername());
        newUser.put("password", user.getPassword());
        String jsonStr = JSON.toJSONString(newUser);
        return jsonStr;
    }
}

